package com.ksyzt.ksite2.client.news;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.EventTarget;
import com.google.gwt.dom.client.Style;
import com.google.gwt.dom.client.Style.Position;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.MouseOutEvent;
import com.google.gwt.event.dom.client.MouseOutHandler;
import com.google.gwt.event.dom.client.MouseOverEvent;
import com.google.gwt.event.dom.client.MouseOverHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.Widget;
import com.ksyzt.gwt.client.common.MessageTextBox;
import com.ksyzt.gwt.client.event.MessageEvent;
import com.ksyzt.gwt.client.event.MessageHandler;
import com.ksyzt.ksite2.client.common.ServerProxy;
import com.ksyzt.ksite2.shared.modle.NEWSObj;
import com.ksyzt.ksite2.shared.modle.VW_NEWSObj;

public class PictureBox extends Composite {

	public static PopupPanel g_panel;
	public static MessageTextBox g_editor;
	public static PictureInfoEditor g_infoEditor;
	public static DialogBox g_panel2;
	private static PictureBoxUiBinder uiBinder = GWT
			.create(PictureBoxUiBinder.class);

	interface PictureBoxUiBinder extends UiBinder<Widget, PictureBox> {
	}

	private AsyncCallback<Void> m_delete_callback = new AsyncCallback<Void>() {

		@Override
		public void onSuccess(Void result) {

			PictureBox.this.removeFromParent();
			PictureBox.hideEditor();
		}

		@Override
		public void onFailure(Throwable caught) {
			message(caught.getMessage());

		}
	};
	private MessageHandler m_operator_handler = new MessageHandler() {

		@Override
		public void onMessage(Object sender, Integer message, Object value) {
			if (message == MessageEvent.DELETE) {

				ServerProxy.SYS_MANAGER.deleteNews(PictureBox.this.m_n.getID(),
						m_delete_callback);
			} else if (message == MessageEvent.EDIT) {

				sureInfoDialog();
				g_infoEditor.init(m_n);
				g_panel2.show();
				g_panel2.center();

			}
		}
	};
	private MessageHandler m_info_editor_handler = new MessageHandler() {

		@Override
		public void onMessage(Object sender, Integer message, Object value) {
			if (MessageEvent.OK == message) {
				VW_NEWSObj n = (VW_NEWSObj) value;
				lbTITLE.setText(n.getTITLE());
				m_n.setTITLE(n.getTITLE());
			}
			g_panel2.hide();
		}
	};

	protected void sureInfoDialog() {
		if (g_panel2 == null) {
			g_panel2 = new DialogBox();
			g_panel2.setGlassEnabled(true);
			g_panel2.setModal(true);
			g_panel2.setHTML("修改图片信息");
			g_infoEditor = new PictureInfoEditor();
			g_infoEditor.addMessageHandler(m_info_editor_handler);
			g_panel2.setWidget(g_infoEditor);
		}
	}

	private void showOperator() {

		PictureOperator op = null;

		if (g_panel == null) {
			op = new PictureOperator();
			g_panel = new PopupPanel();

			g_panel.add(op);
		} else {
			op = (PictureOperator) g_panel.getWidget();
		}

		op.addMessageHandler(m_operator_handler);
		int left = this.getAbsoluteLeft();
		int top = this.getAbsoluteTop();
		g_panel.setPopupPosition(left + 5, top + 5);
		g_panel.show();
	}

	@UiField
	FlowPanel flow;

	private MouseOverHandler m_mouse_over = new MouseOverHandler() {

		@Override
		public void onMouseOver(MouseOverEvent event) {
			showOperator();

		}
	};
	private MouseOutHandler m_mouse_out = new MouseOutHandler() {

		@Override
		public void onMouseOut(MouseOutEvent event) {

			EventTarget ev = event.getRelatedTarget();
			if (Element.is(ev)) {
				Element e = ev.cast();
				if (e.equals(g_panel.getElement())) {

				} else {
					g_panel.hide();
				}
			}

		}
	};
	private AsyncCallback<VW_NEWSObj> m_update_callback = new AsyncCallback<VW_NEWSObj>() {

		@Override
		public void onSuccess(VW_NEWSObj result) {

		}

		@Override
		public void onFailure(Throwable caught) {
			message(caught.getMessage());

		}
	};

	public void message(String message) {
		Window.alert(message);
	}

	private MessageHandler m_editor_handler = new MessageHandler() {

		@Override
		public void onMessage(Object sender, Integer message, Object value) {
			if (message == MessageEvent.CANCEL) {
				g_editor.removeFromParent();
				// lbTITLE.setVisible(true);
			} else if (message == MessageEvent.RETURN) {
				String newVal = g_editor.getValue();
				g_editor.removeFromParent();
				lbTITLE.setText(newVal);
				m_n.setTITLE(newVal);
				NEWSObj n = new NEWSObj();
				n.setID(m_n.getID());
				n.setTITLE(newVal);
				ServerProxy.SYS_MANAGER.submitNews(n, m_update_callback);
			}

		}
	};
	private ClickHandler m_title_clicked = new ClickHandler() {

		@Override
		public void onClick(ClickEvent event) {
			if (g_editor == null) {
				g_editor = new MessageTextBox();
				Style s = g_editor.getElement().getStyle();
				s.setPosition(Position.ABSOLUTE);
				s.setPadding(0, Unit.PX);
				s.setMargin(0, Unit.PX);
			}

			g_editor.setValue(m_n.getTITLE());
			g_editor.setWidth(lbTITLE.getOffsetWidth() + "px");
			g_editor.setHeight(lbTITLE.getOffsetHeight() + "px");
			if (g_editor.isAttached()) {
				g_editor.removeFromParent();
			}
			int left = lbTITLE.getAbsoluteLeft() - flow.getAbsoluteLeft();
			int top = lbTITLE.getAbsoluteTop() - flow.getAbsoluteTop();

			// lbTITLE.setVisible(false);
			flow.add(g_editor);
			Style s = g_editor.getElement().getStyle();
			s.setLeft(left, Unit.PX);
			s.setTop(top, Unit.PX);
			g_editor.addMessageHandler(m_editor_handler);
			g_editor.setFocus(true);

		}
	};

	public PictureBox() {

		initWidget(uiBinder.createAndBindUi(this));

		this.addDomHandler(m_mouse_over, MouseOverEvent.getType());
		this.addDomHandler(m_mouse_out, MouseOutEvent.getType());
		lbTITLE.addClickHandler(m_title_clicked);
		flow.getElement().getStyle().setPosition(Position.RELATIVE);
	}

	@UiField
	Image imgPICURE;
	@UiField
	Label lbTITLE;
	VW_NEWSObj m_n;

	public void renderPicture(VW_NEWSObj n) {
		m_n = n;
		String icon = n.getICON();
		if (icon != null && icon.length() > 0) {
			icon = icon.substring(0, icon.indexOf(".")) + "_s.jpg";
		}
		icon=GWT.getModuleBaseURL() + "../" + icon;
		GWT.log(icon);
		imgPICURE.setUrl(icon);
		lbTITLE.setText(n.getTITLE());

	}

	public static void hideEditor() {
		if (g_editor != null) {
			g_editor.removeFromParent();
		}

	}
}
